package com.grouplay.dao;

import com.grouplay.entity.TransferRecord;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("transferRecordDao")
public interface ITransferRecordDao {

    @Insert("insert into transferRecord (sourceId, targetId, groupId, money, date) " +
            "values (#{sourceId}, #{targetId}, #{groupId}, #{money}, #{date})")
    void insert(TransferRecord transferRecord);


    @Select("select \n" +
            "us.name sourceName,\n" +
            "ut.name targetName,\n" +
            "tr.groupName groupName,\n" +
            "tr.money money,\n" +
            "tr.date date,\n" +
            "isTransferOut\n" +
            "from (\n" +
            "select \n" +
            "t.id, \n" +
            "t.sourceId,\n" +
            "t.targetId,\n" +
            "`group`.groupName,\n" +
            "t.money,\n" +
            "t.date, \n" +
            "1 isTransferOut \n" +
            "from \n" +
            "transferRecord t \n" +
            "left outer join `group` on (`group`.id = t.groupId)  \n" +
            "where \n" +
            "t.sourceId = #{uid} \n" +
            "union all \n" +
            "select \n" +
            "t.id, \n" +
            "t.sourceId,\n" +
            "t.targetId,\n" +
            "`group`.groupName,\n" +
            "t.money, \n" +
            "t.date, \n" +
            "0 isTransferOut \n" +
            "from \n" +
            "transferRecord t \n" +
            "left outer join `group` on (`group`.id = t.groupId)  \n" +
            "where \n" +
            "t.targetId = #{uid}\n" +
            ") as tr,\n" +
            "`user` us,\n" +
            "`user` ut  \n" +
            "where \n" +
            "tr.sourceId = us.id and \n" +
            "tr.targetId = ut.id \n" +
            "order by date desc")
    List<TransferRecord> findMyTransferRecordsByUid(Integer uid);
}
